<?php

namespace App\Cache;

use App\Model\WxUserModel;

class UserCache extends Cache
{
    protected string $primaryKey = 'user:';

    // 60 * 60 * 24 * 15 = 15天 = 1296000 秒
    protected int $timeout = 1296000;

    public function getByOpenid(string $openid): mixed
    {
        if ($this->has($openid)) {
            return $this->get($openid);
        } else {
            $user = WxUserModel::where('openid', $openid)->first();
            $this->set($openid, $user);
            return $user;
        }
    }
}